package ${package}.controller;

import cn.hutool.core.lang.Assert;
import com.dragon.PageUtil.JpaPage;
import com.dragon.bean.BeanCopy;
import com.dragon.config.exception.exception.GlobalException;
import ${package}.controller.dto.${className}AddDTO;
import ${package}.controller.dto.${className}SearchDTO;
import ${package}.controller.dto.${className}UpdateDTO;
import ${package}.controller.vo.${className}ExportVO;
import ${package}.controller.vo.${className}VO;
import ${package}.entity.${className}Entity;
import ${package}.service.${className}Service;
import com.dragon.field.FieldUtil;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import com.dragon.results.util.response.PageVO;
import com.dragon.results.util.response.SortVO;
import com.dragon.time.DateTimeUtils;
import com.dragon.util.ExcelExportUtil;
import com.dragon.util.JPASpecification;
import com.dragon.util.JpaUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ${tableComment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${since}
 */
@Slf4j
@Api(tags = "${tableComment} 相关接口")
@RestController
@RequestMapping("/${packName}")
@RequiredArgsConstructor
@org.springframework.validation.annotation.Validated
public class ${className}Controller {

    private final ${className}Service ${packName}Service;

    <#-- 自定义方法 -->
    @PostMapping("export")
    @ApiOperation("1]导出数据")
    @ApiOperationSupport(order = 1)
    public void export(HttpServletResponse response, @RequestBody ${className}SearchDTO searchDTO) {
        JPASpecification<${className}Entity> specification = JpaUtils.constructQuery(searchDTO, ${className}Entity.class);
        SortVO sort = new SortVO();
        sort.setOrderBy(FieldUtil.getFieldName(${className}Entity::getCreateTime, false));
        sort.setOrderDesc(1);
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(1);
        pageVO.setSize(1000);
        long totalPages = -1;
        List<${className}ExportVO> exportList = new ArrayList<>();
        Pageable pageable = JpaPage.setPageSort(pageVO, sort);
        do {
            Page<${className}Entity> page = ${packName}Service.findAllPage(specification, pageable);
            if (totalPages == -1) {
                totalPages = page.getTotalPages();
            }
            List<${className}ExportVO> copied = BeanCopy.copyList(page.getContent(), ${className}ExportVO.class);
            exportList.addAll(copied);
            pageVO.setCurrent(pageVO.getCurrent() + 1);
            pageable = JpaPage.setPageSort(pageVO, sort);
        } while (pageVO.getCurrent() <= totalPages);
        ExcelExportUtil.export(response, DateTimeUtils.getCurrentDate() + "_${tableComment}", exportList);
    }

    @PostMapping("add")
    @ApiOperation("2]${tableComment!}添加")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> add(@RequestBody @Valid ${className}AddDTO addDTO) {
        ${className}Entity entity = ${className}AddDTO.to(addDTO, ${className}Entity.class);
        ${packName}Service.saveByBean(entity);
        return HttpResult.ok("添加成功");
    }

    @PutMapping("update")
    @ApiOperation("3]${tableComment}修改")
    @ApiOperationSupport(order = 3)
    public HttpResult<String> update(@RequestBody @Valid ${className}UpdateDTO updateDTO) {
        ${className}Entity entity = ${packName}Service.findById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        BeanCopy.copy(updateDTO, entity);
        ${packName}Service.updateByBean(entity);
        return HttpResult.ok("修改成功");
    }

    @GetMapping("search")
    @ApiOperation("4]${tableComment}分页查询")
    @ApiOperationSupport(order = 4)
    public HttpResult<ResultPage<List<${className}VO>>> search(@Valid ${className}SearchDTO searchDTO) {
        JPASpecification<${className}Entity> specification = JpaUtils.constructQuery(searchDTO, ${className}Entity.class);
        SortVO sort = new SortVO();
        sort.setOrderBy(FieldUtil.getFieldName(${className}Entity::getCreateTime, false));
        sort.setOrderDesc(1);
        Pageable pageable = JpaPage.setPageSort(searchDTO.getPage(), sort);
        Page<${className}Entity> page = ${packName}Service.findAllPage(specification, pageable);
        ResultPage<List<${className}VO>> resultPage = JpaPage.copyJpaPage(page, ${className}VO.class);
        return HttpResult.okForData(resultPage);
    }

    @DeleteMapping("delete")
    @ApiOperation("5]${tableComment}删除")
    @ApiOperationSupport(order = 5)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        ${packName}Service.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }
}